{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import csv\n",
    "import wfdb\n",
    "import heartpy as hp\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import scipy.signal as ssignal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cheby(ppg, fs, order=4, rs=20, btype='low'):\n",
    "    nyquist = 0.5 * fs\n",
    "    cutoff = 10 / nyquist\n",
    "    rs, order = 20, 4\n",
    "    b, a = ssignal.cheby2(N=order, rs=rs, Wn=cutoff, btype='low')\n",
    "    return ssignal.filtfilt(b, a, ppg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "119976\n"
     ]
    },
    {
     "ename": "BadSignalWarning",
     "evalue": "\n----------------\nCould not determine best fit for given signal. Please check the source signal.\n Probable causes:\n- detected heart rate falls outside of bpmmin<->bpmmax constraints\n- no detectable heart rate present in signal\n- very noisy signal (consider filtering and scaling)\nIf you're sure the signal contains heartrate data, consider filtering and/or scaling first.\n----------------\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mBadSignalWarning\u001b[0m                          Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 6\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mlen\u001b[39m(record\u001b[38;5;241m.\u001b[39mp_signal\u001b[38;5;241m.\u001b[39mflatten()))\n\u001b[0;32m      4\u001b[0m signal \u001b[38;5;241m=\u001b[39m cheby(record\u001b[38;5;241m.\u001b[39mp_signal\u001b[38;5;241m.\u001b[39mflatten(),record\u001b[38;5;241m.\u001b[39mfs,order \u001b[38;5;241m=\u001b[39m\u001b[38;5;241m4\u001b[39m)\n\u001b[1;32m----> 6\u001b[0m wb,m \u001b[38;5;241m=\u001b[39m \u001b[43mhp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess\u001b[49m\u001b[43m(\u001b[49m\u001b[43msignal\u001b[49m\u001b[43m,\u001b[49m\u001b[43mrecord\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32mc:\\Users\\toha2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\heartpy\\heartpy.py:289\u001b[0m, in \u001b[0;36mprocess\u001b[1;34m(hrdata, sample_rate, windowsize, report_time, calc_freq, freq_method, welch_wsize, freq_square, interp_clipping, clipping_scale, interp_threshold, hampel_correct, bpmmin, bpmmax, reject_segmentwise, high_precision, high_precision_fs, breathing_method, clean_rr, clean_rr_method, measures, working_data)\u001b[0m\n\u001b[0;32m    285\u001b[0m working_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msample_rate\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m=\u001b[39m sample_rate\n\u001b[0;32m    287\u001b[0m rol_mean \u001b[38;5;241m=\u001b[39m rolling_mean(hrdata, windowsize, sample_rate)\n\u001b[1;32m--> 289\u001b[0m working_data \u001b[38;5;241m=\u001b[39m \u001b[43mfit_peaks\u001b[49m\u001b[43m(\u001b[49m\u001b[43mhrdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrol_mean\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msample_rate\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbpmmin\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbpmmin\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m    290\u001b[0m \u001b[43m                         \u001b[49m\u001b[43mbpmmax\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbpmmax\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mworking_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mworking_data\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    292\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m high_precision:\n\u001b[0;32m    293\u001b[0m     working_data \u001b[38;5;241m=\u001b[39m interpolate_peaks(hrdata, working_data[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpeaklist\u001b[39m\u001b[38;5;124m'\u001b[39m], sample_rate\u001b[38;5;241m=\u001b[39msample_rate,\n\u001b[0;32m    294\u001b[0m                                      desired_sample_rate\u001b[38;5;241m=\u001b[39mhigh_precision_fs, working_data\u001b[38;5;241m=\u001b[39mworking_data)\n",
      "File \u001b[1;32mc:\\Users\\toha2\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\heartpy\\peakdetection.py:305\u001b[0m, in \u001b[0;36mfit_peaks\u001b[1;34m(hrdata, rol_mean, sample_rate, bpmmin, bpmmax, working_data)\u001b[0m\n\u001b[0;32m    303\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m working_data\n\u001b[0;32m    304\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m--> 305\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m BadSignalWarning(\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m----------------\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mCould not determine best fit for \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[0;32m    306\u001b[0m \u001b[38;5;124mgiven signal. Please check the source signal.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m Probable causes:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m- detected heart rate falls \u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[0;32m    307\u001b[0m \u001b[38;5;124moutside of bpmmin<->bpmmax constraints\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m- no detectable heart rate present in signal\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[0;32m    308\u001b[0m \u001b[38;5;124m- very noisy signal (consider filtering and scaling)\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mIf you\u001b[39m\u001b[38;5;130;01m\\'\u001b[39;00m\u001b[38;5;124mre sure the signal contains heart\u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[0;32m    309\u001b[0m \u001b[38;5;124mrate data, consider filtering and/or scaling first.\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m----------------\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mBadSignalWarning\u001b[0m: \n----------------\nCould not determine best fit for given signal. Please check the source signal.\n Probable causes:\n- detected heart rate falls outside of bpmmin<->bpmmax constraints\n- no detectable heart rate present in signal\n- very noisy signal (consider filtering and scaling)\nIf you're sure the signal contains heartrate data, consider filtering and/or scaling first.\n----------------\n"
     ]
    }
   ],
   "source": [
    "record = wfdb.rdrecord(r\"D:\\Proga\\AML\\datasets\\senssmart\\sens\\senssmarttech-database-of-cardiovascular-signals-synchronously-recorded-by-an-electrocardiograph-phonocardiograph-photoplethysmograph-and-accelerometer-1.0.0\\WFDB\\1_10-11-48_ppg\")\n",
    "record.__dict__\n",
    "print(len(record.p_signal.flatten()))\n",
    "signal = cheby(record.p_signal.flatten(),record.fs,order =4)\n",
    "\n",
    "wb,m = hp.process(signal,record.fs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
